package org.dcits.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.dcits.model.ApplyCard;
import org.dcits.service.ApplyCardService;
import org.dcits.util.Jdbc;
import org.dcits.util.Md5Util;
import org.dcits.util.ToCardNoUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;

@Controller
public class ApplyCardController {
	
	@Autowired
	private ApplyCardService  applyCardService;
	
	
	@RequestMapping("apply")
	public String findAll(Map<String, Object> map , HttpSession hs) {
	
		int userId = (int)hs.getAttribute("userId");
		List<ApplyCard> findAll = applyCardService.findAll();
		List<ApplyCard> userMsg = applyCardService.userMsg(userId);
		System.out.println(userMsg);
		map.put("branch", findAll);
		map.put("msg", userMsg);
		return "applyCard";
		
	}
	
	@RequestMapping("changeShi")
	@ResponseBody
	public Map<String, Object> changeShi(Integer citys) {
	
		
		List<ApplyCard> changeShi = applyCardService.changeShi(citys);
		
		return outResult("200", "", changeShi);
		
	}
	
	@RequestMapping("submit")
	@ResponseBody
	public Map<String, Object> submit(String data ,HttpSession hs) {
		String pwd = data ;
		
		int userId = (int)hs.getAttribute("userId");
		String guid = ToCardNoUtil.getGuid();
		
		boolean r = applyCardService.insertCard(guid,pwd,userId);
		if(r) {
			hs.setAttribute("acctNo", guid);
			return outResult("200", "",guid );
		}else {
			return outResult("400", "","" );
		}
		
	}
	
	private Map<String, Object> outResult(String code, String msg, Object data){
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("code", code);
		map.put("msg", msg);
		map.put("data", data);
		return map;
	}
}
